package com.hyt.it.ogt.common.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.hyt.core.util.GeneralProperties;
import com.hyt.swagger.ApiVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description: com.dx.config ConditionalOnProperty(name = "swagger.enable",
 * havingValue = "true")
 * 是否开启swagger，正式环境一般是需要关闭的（避免不必要的漏洞暴露！），可根据springboot的多环境配置进行设置
 *
 * @author yaojian
 * @version 1.0
 * @since 2020/5/30
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

	@SuppressWarnings("deprecation")
	@Bean
	public Docket v2311Api() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name("access_token").description("token").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
		pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_2).groupName(ApiVersionConstant.V2_3_11).apiInfo(v2311apiInfo()).select().apis(input -> {
			ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
			if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.V2_3_11)) {
				return true;
			}
			return false;
		}).paths(PathSelectors.any()).build().globalOperationParameters(pars);
	}

	@SuppressWarnings("deprecation")
	@Bean
	public Docket v241Api() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name("access_token").description("token").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
		pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_2).groupName(ApiVersionConstant.V2_4_1).apiInfo(v241apiInfo()).select().apis(input -> {
			ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
			if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.V2_4_1)) {
				return true;
			}
			return false;
		}).paths(PathSelectors.any()).build().globalOperationParameters(pars);
	}

	@SuppressWarnings("deprecation")
	@Bean
	public Docket v242Api() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name("access_token").description("token").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
		pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_2).groupName(ApiVersionConstant.V2_4_2).apiInfo(v242apiInfo()).select().apis(input -> {
			ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
			if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.V2_4_2)) {
				return true;
			}
			return false;
		}).paths(PathSelectors.any()).build().globalOperationParameters(pars);
	}

	@SuppressWarnings("deprecation")
	@Bean
	public Docket v249Api() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name("access_token").description("token").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
		pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_2).groupName(ApiVersionConstant.V2_4_9).apiInfo(v249apiInfo()).select().apis(input -> {
			ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
			if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.V2_4_9)) {
				return true;
			}
			return false;
		}).paths(PathSelectors.any()).build().globalOperationParameters(pars);
	}

	@SuppressWarnings("deprecation")
	@Bean
	public Docket v2410Api() {
		ParameterBuilder tokenPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name("access_token").description("token").modelRef(new ModelRef("string")).parameterType("query").required(false).build();
		pars.add(tokenPar.build());
		return new Docket(DocumentationType.SWAGGER_2).groupName(ApiVersionConstant.V2_4_10).apiInfo(v2410apiInfo()).select().apis(input -> {
			ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
			if (apiVersion != null && Arrays.asList(apiVersion.group()).contains(ApiVersionConstant.V2_4_10)) {
				return true;
			}
			return false;
		}).paths(PathSelectors.any()).build().globalOperationParameters(pars);
	}

	private ApiInfo v2311apiInfo() {
		String title = GeneralProperties.getProperty("hyt.api.doc.title", "海云天云考试");
		String description = GeneralProperties.getProperty("hyt.api.doc.description", "海云天云考试系统对外接口");
		String termsOfServiceUrl = GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl", "http://www.seaskylight.com");
		String contactName = GeneralProperties.getProperty("hyt.api.doc.contact.name", "云考试-考试项目组");
		String contactUrl = GeneralProperties.getProperty("hyt.api.doc.contact.url", "http://www.seaskylight.com");
		String contactEamil = GeneralProperties.getProperty("hyt.api.doc.contact.email", "linjd@seaskylight.com");
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl).contact(new Contact(contactName, contactUrl, contactEamil))
				.version(ApiVersionConstant.V2_3_11).build();
	}

	private ApiInfo v241apiInfo() {
		String title = GeneralProperties.getProperty("hyt.api.doc.title", "海云天云考试");
		String description = GeneralProperties.getProperty("hyt.api.doc.description", "海云天云考试系统对外接口");
		String termsOfServiceUrl = GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl", "http://www.seaskylight.com");
		String contactName = GeneralProperties.getProperty("hyt.api.doc.contact.name", "云考试-考试项目组");
		String contactUrl = GeneralProperties.getProperty("hyt.api.doc.contact.url", "http://www.seaskylight.com");
		String contactEamil = GeneralProperties.getProperty("hyt.api.doc.contact.email", "linjd@seaskylight.com");
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl).contact(new Contact(contactName, contactUrl, contactEamil))
				.version(ApiVersionConstant.V2_4_1).build();
	}

	private ApiInfo v242apiInfo() {
		String title = GeneralProperties.getProperty("hyt.api.doc.title", "海云天云考试");
		String description = GeneralProperties.getProperty("hyt.api.doc.description", "海云天云考试系统对外接口");
		String termsOfServiceUrl = GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl", "http://www.seaskylight.com");
		String contactName = GeneralProperties.getProperty("hyt.api.doc.contact.name", "云考试-考试项目组");
		String contactUrl = GeneralProperties.getProperty("hyt.api.doc.contact.url", "http://www.seaskylight.com");
		String contactEamil = GeneralProperties.getProperty("hyt.api.doc.contact.email", "linjd@seaskylight.com");
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl).contact(new Contact(contactName, contactUrl, contactEamil))
				.version(ApiVersionConstant.V2_4_2).build();
	}

	private ApiInfo v249apiInfo() {
		String title = GeneralProperties.getProperty("hyt.api.doc.title", "海云天云考试");
		String description = GeneralProperties.getProperty("hyt.api.doc.description", "海云天云考试系统对外接口");
		String termsOfServiceUrl = GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl", "http://www.seaskylight.com");
		String contactName = GeneralProperties.getProperty("hyt.api.doc.contact.name", "云考试-考试项目组");
		String contactUrl = GeneralProperties.getProperty("hyt.api.doc.contact.url", "http://www.seaskylight.com");
		String contactEamil = GeneralProperties.getProperty("hyt.api.doc.contact.email", "linjd@seaskylight.com");
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl).contact(new Contact(contactName, contactUrl, contactEamil))
				.version(ApiVersionConstant.V2_4_9).build();
	}

	private ApiInfo v2410apiInfo() {
		String title = GeneralProperties.getProperty("hyt.api.doc.title", "海云天云考试");
		String description = GeneralProperties.getProperty("hyt.api.doc.description", "海云天云考试系统对外接口");
		String termsOfServiceUrl = GeneralProperties.getProperty("hyt.api.doc.termsOfServiceUrl", "http://www.seaskylight.com");
		String contactName = GeneralProperties.getProperty("hyt.api.doc.contact.name", "云考试-考试项目组");
		String contactUrl = GeneralProperties.getProperty("hyt.api.doc.contact.url", "http://www.seaskylight.com");
		String contactEamil = GeneralProperties.getProperty("hyt.api.doc.contact.email", "linjd@seaskylight.com");
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl).contact(new Contact(contactName, contactUrl, contactEamil))
				.version(ApiVersionConstant.V2_4_10).build();
	}
}
